package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        /**
         * 1.创建setmeal对象
         * 2.将setmealDTO对象属性复制到setmeal对象中
         * 3.将setmeal插入到表中（需要插入以后的返回值id）
         * 4.从setmealDTO中获取dish对象，遍历进行插入
         */
        Setmeal setmeal =new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);


        setmealMapper.insert(setmeal);


        String name = setmeal.getName();
        long id = setmealMapper.getbyName(name);


        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
            setmealDishMapper.insertSetmealDish(setmealDish);
        });






    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //使用PageHelper插件实现分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page page=setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Long[] ids) {
        /**
         * 1.遍历数组，获取id
         * 2.判断套餐状态，如果状态是启售状态，则不能删除
         * 3.如果套餐与菜品关联，则删除setmeal_dish表中的菜品数据
         * 4.删除setmeal表中的套餐数据
         */
        for (Long id : ids) {
            Setmeal setmeal=setmealMapper.findById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                //启售状态
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            List<SetmealDish> setmealDishes=setmealDishMapper.getByDishId(id);

            String name = setmeal.getName();
            long setmeal_id = setmealMapper.getbyName(name);
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteBySetmealId(setmeal_id);


        }

    }

    @Override
    public SetmealVO getByIdWithDishes(Long id) {
        /**
         * 1.去setmeal表中查询套餐数据
         * 2.去setmeal_dish表中查询套餐菜品数据
         * 3.将两个数据封装到SetmealVO中并返回
         */
        Setmeal setmeal=setmealMapper.findById(id);
        List<SetmealDish> setmealDishes=setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO=new SetmealVO();
        //拷贝套餐数据
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Transactional
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        /**
         * 1.修改setmeal表数据(把setmealDTO-->setmeal)
         * 2.删除setmeal_dish表数据
         * 3.重新插入setmeal_dish表数据
         */
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();

        dishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));

        setmealDishMapper.insertBatch(dishes);


    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @Transactional
    @Override
    public void startOrStop(Integer status, Long id) {

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }



}
